<template>
    <div class="box">
        <header class="header">
          <van-nav-bar title="添加地址" left-arrow @click-left="$router.back()">
          </van-nav-bar>
        </header>
        <div class="content" ref="Info">
            <van-address-edit
                :area-list="areaList"
                show-postal
                show-set-default
                show-search-result
                save-button-text="保存并使用"
                :search-result="searchResult"
                :area-columns-placeholder="['请选择', '请选择', '请选择']"
                @save="onSave"
            >
            </van-address-edit>
        </div>
    </div>
</template>

<script>
import Vue from 'vue'
import { NavBar, Cell, Button, Icon, AddressEdit, Toast, Area } from 'vant'
import { areaList } from '@vant/area-data'
import { newAddress, updateOrderAddress } from '@/api/order'

Vue.use(NavBar)
Vue.use(Cell)
Vue.use(Button)
Vue.use(Icon)
Vue.use(AddressEdit)
Vue.use(Toast)
Vue.use(Area)
export default {
  data () {
    return {
      searchResult: [],
      areaList,
      orderTime: ''
    }
  },
  mounted () {
    this.orderTime = this.$route.query.time
    console.log(this.orderTime)
  },
  methods: {
    async onSave (addressInfo) {
      addressInfo.userid = localStorage.getItem('userid')
      addressInfo.time = this.orderTime
      var res = await newAddress(addressInfo)
      // console.log(res)
      if (res.data.code === '200') {
        var resuporder = await updateOrderAddress(addressInfo)
        console.log(resuporder)
        if (resuporder.data.code === '200') {
          this.$router.push({ path: 'orderIndex', query: { time: this.orderTime } })
          Toast('成功使用地址')
        }
        // this.$router.go(-1)
      }
      // console.log(addressInfo)
    }
  }
}
</script>

<style lang="stylus" scoped>
</style>
